package androidx.test.internal.runner;

import dalvik.system.DexFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/* loaded from: classes.dex */
public class ClassPathScanner {
    private final Set<String> classPath;

    /* loaded from: classes.dex */
    public static class AcceptAllFilter implements ClassNameFilter {
        @Override // androidx.test.internal.runner.ClassPathScanner.ClassNameFilter
        public boolean accept(String className) {
            return true;
        }
    }

    /* loaded from: classes.dex */
    public interface ClassNameFilter {
        boolean accept(String className);
    }

    /* loaded from: classes.dex */
    public static class ChainedClassNameFilter implements ClassNameFilter {
        private final List<ClassNameFilter> filters = new ArrayList();

        public void add(ClassNameFilter filter) {
            this.filters.add(filter);
        }

        public void addAll(ClassNameFilter... filters) {
            this.filters.addAll(Arrays.asList(filters));
        }

        @Override // androidx.test.internal.runner.ClassPathScanner.ClassNameFilter
        public boolean accept(String className) {
            for (ClassNameFilter classNameFilter : this.filters) {
                if (!classNameFilter.accept(className)) {
                    return false;
                }
            }
            return true;
        }
    }

    /* loaded from: classes.dex */
    public static class ExternalClassNameFilter implements ClassNameFilter {
        @Override // androidx.test.internal.runner.ClassPathScanner.ClassNameFilter
        public boolean accept(String pathName) {
            return !pathName.contains("$");
        }
    }

    /* loaded from: classes.dex */
    public static class InclusivePackageNamesFilter implements ClassNameFilter {
        private final Collection<String> pkgNames;

        /* JADX INFO: Access modifiers changed from: package-private */
        public InclusivePackageNamesFilter(Collection<String> pkgNames) {
            this.pkgNames = new ArrayList(pkgNames.size());
            for (String str : pkgNames) {
                if (!str.endsWith(".")) {
                    this.pkgNames.add(String.format("%s.", str));
                } else {
                    this.pkgNames.add(str);
                }
            }
        }

        @Override // androidx.test.internal.runner.ClassPathScanner.ClassNameFilter
        public boolean accept(String pathName) {
            for (String str : this.pkgNames) {
                if (pathName.startsWith(str)) {
                    return true;
                }
            }
            return false;
        }
    }

    /* loaded from: classes.dex */
    public static class ExcludePackageNameFilter implements ClassNameFilter {
        private final String pkgName;

        /* JADX INFO: Access modifiers changed from: package-private */
        public ExcludePackageNameFilter(String pkgName) {
            if (!pkgName.endsWith(".")) {
                this.pkgName = String.format("%s.", pkgName);
            } else {
                this.pkgName = pkgName;
            }
        }

        @Override // androidx.test.internal.runner.ClassPathScanner.ClassNameFilter
        public boolean accept(String pathName) {
            return !pathName.startsWith(this.pkgName);
        }
    }

    /* loaded from: classes.dex */
    static class ExcludeClassNamesFilter implements ClassNameFilter {
        private Set<String> excludedClassNames;

        public ExcludeClassNamesFilter(Set<String> excludedClassNames) {
            this.excludedClassNames = excludedClassNames;
        }

        @Override // androidx.test.internal.runner.ClassPathScanner.ClassNameFilter
        public boolean accept(String className) {
            return !this.excludedClassNames.contains(className);
        }
    }

    public ClassPathScanner(String... paths) {
        this(Arrays.asList(paths));
    }

    public ClassPathScanner(Collection<String> paths) {
        HashSet hashSet = new HashSet();
        this.classPath = hashSet;
        hashSet.addAll(paths);
    }

    private void addEntriesFromPath(Set<String> entryNames, String path, ClassNameFilter filter) throws IOException {
        DexFile dexFile;
        DexFile dexFile2 = null;
        try {
            dexFile = new DexFile(path);
        } catch (Throwable th) {
            th = th;
        }
        try {
            Enumeration<String> dexEntries = getDexEntries(dexFile);
            while (dexEntries.hasMoreElements()) {
                String nextElement = dexEntries.nextElement();
                if (filter.accept(nextElement)) {
                    entryNames.add(nextElement);
                }
            }
            dexFile.close();
        } catch (Throwable th2) {
            th = th2;
            dexFile2 = dexFile;
            if (dexFile2 != null) {
                dexFile2.close();
            }
            throw th;
        }
    }

    Enumeration<String> getDexEntries(DexFile dexFile) {
        return dexFile.entries();
    }

    public Set<String> getClassPathEntries(ClassNameFilter filter) throws IOException {
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        for (String str : this.classPath) {
            addEntriesFromPath(linkedHashSet, str, filter);
        }
        return linkedHashSet;
    }
}
